package org.example.oa.view;

import com.jiazhong.mybatis.dao.BaseDAO;
import org.example.oa.bean.Emp;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainView extends JFrame {

    private JPanel topPanel, bottomPanel;
    private JScrollPane mainPane;
    private JButton saveButton, updateButton, deleteButton, searchButton;
    private JButton firstButton, lastButton, prevButton, nextButton;
    private JTextField searchField;
    private JTable jTable;
    private BaseDAO bdao;
    private Object[][] data;
    private String[] title;

    private int nowPage = 1;
    private int allPage;

    public MainView() {
        bdao = new BaseDAO("a123456", "oa");
        setTitle("主界面");
        topPanel = new JPanel();
        bottomPanel = new JPanel();
        saveButton = new JButton("添加");
        updateButton = new JButton("修改");
        deleteButton = new JButton("删除");
        searchButton = new JButton("搜索");
        firstButton = new JButton("首页");
        lastButton = new JButton("末页");
        prevButton = new JButton("上一页");
        nextButton = new JButton("下一页");
        searchField = new JTextField(10);
        // 从数据库中获取到表格的数据（先获取到所有）
        Object[][] all = bdao.findAll("select * from emp", Emp.class);
        int total = all.length;
        allPage = total % 10 == 0 ? (total / 10) : (total / 10 + 1);
        data = bdao.findPage("select * from emp limit ?,?", nowPage, Emp.class);
        title = new String[]{"编号", "姓名", "密码", "职位", "入职时间", "工资", "奖金", "部门ID"};
        jTable = new JTable(data, title);
        mainPane = new JScrollPane(jTable);
        topPanel.add(saveButton);
        topPanel.add(updateButton);
        topPanel.add(deleteButton);
        topPanel.add(searchField);
        topPanel.add(searchButton);
        bottomPanel.add(firstButton);
        bottomPanel.add(prevButton);
        bottomPanel.add(nextButton);
        bottomPanel.add(lastButton);

        add(topPanel, BorderLayout.NORTH);
        add(bottomPanel, BorderLayout.SOUTH);
        add(mainPane);

        saveButton.addActionListener(new ShowSaveDialog());
        updateButton.addActionListener(new ShowUpdateDialog());
        deleteButton.addActionListener(new DeleteClick());
        searchButton.addActionListener(new SearchClick());
        firstButton.addActionListener(new FirstClick());
        prevButton.addActionListener(new PrevClick());
        nextButton.addActionListener(new NextClick());
        lastButton.addActionListener(new LastClick());

        setBounds(2, 3, 800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    class ShowSaveDialog implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            new SaveDialog();
        }
    }

    private String id;
    private Object[] selectRow;

    class ShowUpdateDialog implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int row = jTable.getSelectedRow();
            id = data[row][0].toString();
            selectRow = data[row];
            new UpdateDialog();
        }
    }

    class DeleteClick implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showConfirmDialog(MainView.this, "你确定要删除么");
            if (result == 0) {
                String sql = "delete from emp where id=?";
                Object[] params = {data[jTable.getSelectedRow()][0]};
                bdao.delete(sql, params);
                Object[][] all = bdao.findAll("select * from emp", Emp.class);
                String[] title = {"编号", "姓名", "密码", "职位", "入职时间", "工资", "奖金", "部门ID"};
                jTable.setModel(new DefaultTableModel(all, title));
                jTable.repaint();
            }
        }
    }

    class SearchClick implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 1. 获取到文本框的内容
            String searchFieldText = searchField.getText();
            // 2. 将数据传递给数据库
            String sql = "select * from emp where ename like ?";
            Object[] params = {"%" + searchFieldText + "%"};
            Object[][] all = bdao.find(sql, params, Emp.class);
            // 3. 刷新表格
            jTable.setModel(new DefaultTableModel(all, title));
            jTable.repaint();
        }
    }

    class FirstClick implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            nowPage = 1;
            data = bdao.findPage("select * from emp limit ?,?", nowPage, Emp.class);
            jTable.setModel(new DefaultTableModel(data, title));
            jTable.repaint();
        }
    }

    class LastClick implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            nowPage = allPage;
            data = bdao.findPage("select * from emp limit ?,?", nowPage, Emp.class);
            jTable.setModel(new DefaultTableModel(data, title));
            jTable.repaint();
        }
    }

    class PrevClick implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (nowPage == 1) {
                JOptionPane.showMessageDialog(MainView.this, "这个就是首页了");
                return;
            }
            nowPage = nowPage - 1;
            data = bdao.findPage("select * from emp limit ?,?", nowPage, Emp.class);
            jTable.setModel(new DefaultTableModel(data, title));
            jTable.repaint();
        }
    }
    class NextClick implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (nowPage == allPage) {
                JOptionPane.showMessageDialog(MainView.this, "这个就是末页了");
                return;
            }
            nowPage = nowPage + 1;
            data = bdao.findPage("select * from emp limit ?,?", nowPage, Emp.class);
            jTable.setModel(new DefaultTableModel(data, title));
            jTable.repaint();
        }
    }
    class SaveDialog extends JDialog {

        private JLabel l1, l2, l3, l4, l5, l6, l7;
        private JTextField saveNameField, saveHiredateField, saveSalField, saveCommField;
        private JPasswordField savePasswordField;
        private JComboBox<String> saveJobBox, saveDeptnoBox;
        private JButton button;

        public SaveDialog() {
            super(MainView.this, true);
            setTitle("添加窗体");
            l1 = new JLabel("姓名：");
            l2 = new JLabel("密码：");
            l3 = new JLabel("职位：");
            l4 = new JLabel("入职时间：");
            l5 = new JLabel("工资：");
            l6 = new JLabel("奖金：");
            l7 = new JLabel("部门ID：");
            saveNameField = new JTextField();
            saveCommField = new JTextField();
            saveSalField = new JTextField();
            saveHiredateField = new JTextField();
            savePasswordField = new JPasswordField();
            saveJobBox = new JComboBox<>(new String[]{"程序员", "网页设计", "运维", "财务", "行政", "会计", "出纳"});
            saveDeptnoBox = new JComboBox<>(new String[]{"10", "20", "30", "40", "50"});
            button = new JButton("录入新员工");
            setLayout(null);
            l1.setBounds(10, 30, 80, 30);
            l2.setBounds(10, 70, 80, 30);
            l3.setBounds(10, 110, 80, 30);
            l4.setBounds(10, 150, 80, 30);
            l5.setBounds(10, 190, 80, 30);
            l6.setBounds(10, 230, 80, 30);
            l7.setBounds(10, 270, 80, 30);
            saveNameField.setBounds(100, 30, 120, 30);
            savePasswordField.setBounds(100, 70, 120, 30);
            saveJobBox.setBounds(100, 110, 120, 30);
            saveHiredateField.setBounds(100, 150, 120, 30);
            saveSalField.setBounds(100, 190, 120, 30);
            saveCommField.setBounds(100, 230, 120, 30);
            saveDeptnoBox.setBounds(100, 270, 120, 30);
            button.setBounds(90, 340, 160, 30);
            add(l1);
            add(l2);
            add(l3);
            add(l4);
            add(l5);
            add(l6);
            add(l7);
            add(saveNameField);
            add(saveJobBox);
            add(savePasswordField);
            add(saveCommField);
            add(saveSalField);
            add(saveDeptnoBox);
            add(saveHiredateField);
            add(button);

            button.addActionListener(e -> {
                System.out.println("1");
                // 1. 获取到框的内容
                String name = saveNameField.getText();
                String password = savePasswordField.getText();
                String job = saveJobBox.getSelectedItem().toString();
                String hiredate = saveHiredateField.getText();
                String sal = saveSalField.getText();
                String comm = saveCommField.getText();
                String deptno = saveDeptnoBox.getSelectedItem().toString();
                System.out.println(name);
                System.out.println(job);
                // 2. 调用添加操作
                bdao.save("insert into emp values(null,?,?,?,?,?,?,?)", new Object[]{name, password, job, hiredate, sal, comm, deptno});
                // 3. 刷新表格
                Object[][] all = bdao.findAll("select * from emp", Emp.class);
                String[] title = {"编号", "姓名", "密码", "职位", "入职时间", "工资", "奖金", "部门ID"};
                jTable.setModel(new DefaultTableModel(all, title));
                jTable.repaint();
                // 4. 隐藏窗体
                setVisible(false);
            });
            setBounds(2, 3, 300, 500);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    }

    class UpdateDialog extends JDialog {

        private JLabel l1, l2, l3, l4, l5, l6, l7;
        private JTextField saveNameField, saveHiredateField, saveSalField, saveCommField;
        private JPasswordField savePasswordField;
        private JComboBox<String> saveJobBox, saveDeptnoBox;
        private JButton button;


        public UpdateDialog() {
            super(MainView.this, true);
            setTitle("修改窗体");
            l1 = new JLabel("姓名：");
            l2 = new JLabel("密码：");
            l3 = new JLabel("职位：");
            l4 = new JLabel("入职时间：");
            l5 = new JLabel("工资：");
            l6 = new JLabel("奖金：");
            l7 = new JLabel("部门ID：");
            saveNameField = new JTextField(selectRow[1].toString());
            saveCommField = new JTextField(selectRow[6].toString());
            saveSalField = new JTextField(selectRow[5].toString());
            saveHiredateField = new JTextField(selectRow[4].toString());
            savePasswordField = new JPasswordField(selectRow[2].toString());
            saveJobBox = new JComboBox<>(new String[]{"程序员", "网页设计", "运维", "财务", "行政", "会计", "出纳"});
            saveDeptnoBox = new JComboBox<>(new String[]{"10", "20", "30", "40", "50"});
            button = new JButton("修改员工");
            setLayout(null);
            l1.setBounds(10, 30, 80, 30);
            l2.setBounds(10, 70, 80, 30);
            l3.setBounds(10, 110, 80, 30);
            l4.setBounds(10, 150, 80, 30);
            l5.setBounds(10, 190, 80, 30);
            l6.setBounds(10, 230, 80, 30);
            l7.setBounds(10, 270, 80, 30);
            saveNameField.setBounds(100, 30, 120, 30);
            savePasswordField.setBounds(100, 70, 120, 30);
            saveJobBox.setBounds(100, 110, 120, 30);
            saveHiredateField.setBounds(100, 150, 120, 30);
            saveSalField.setBounds(100, 190, 120, 30);
            saveCommField.setBounds(100, 230, 120, 30);
            saveDeptnoBox.setBounds(100, 270, 120, 30);
            button.setBounds(90, 340, 160, 30);
            add(l1);
            add(l2);
            add(l3);
            add(l4);
            add(l5);
            add(l6);
            add(l7);
            add(saveNameField);
            add(saveJobBox);
            add(savePasswordField);
            add(saveCommField);
            add(saveSalField);
            add(saveDeptnoBox);
            add(saveHiredateField);
            add(button);

            button.addActionListener(e -> {
                // 1. 获取到框的内容
                String name = saveNameField.getText();
                String password = savePasswordField.getText();
                String job = saveJobBox.getSelectedItem().toString();
                String hiredate = saveHiredateField.getText();
                String sal = saveSalField.getText();
                String comm = saveCommField.getText();
                String deptno = saveDeptnoBox.getSelectedItem().toString();
                // 2. 调用添加操作
                String sql = "update emp set ename=?,password=?,job=?,hiredate=?,sal=?,comm=?,deptno=? where id=?";
                bdao.save(sql, new Object[]{name, password, job, hiredate, sal, comm, deptno, id});
                // 3. 刷新表格
                Object[][] all = bdao.findAll("select * from emp", Emp.class);
                String[] title = {"编号", "姓名", "密码", "职位", "入职时间", "工资", "奖金", "部门ID"};
                jTable.setModel(new DefaultTableModel(all, title));
                jTable.repaint();
                // 4. 隐藏窗体
                setVisible(false);
            });
            setBounds(2, 3, 300, 500);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    }
}
